<?php

namespace app\admin\model;

use think\Model;


class Package extends Model
{

    

    

    // 表名
    protected $name = 'package';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'times_format',
        'video_list'
    ];
    protected static function init()
    {
        self::afterWrite(function ($data) {
            self::initPackage($data['id']);
        });
//        setLog(__METHOD__,json_encode($data,JSON_UNESCAPED_UNICODE));
//

    }

    public static function initPackage($id)
    {
        $package = self::find($id);
        $videoList=Pvideo::with('video')->where('package_id',$id)->select();
        setLog(__METHOD__,json_encode($videoList,JSON_UNESCAPED_UNICODE));
        $package->nums=count($videoList);
        $times=0;
        foreach ($videoList as $video)
        {
            $times+=$video->video->times;
        }
        $package->times=$times;
        $package->save();
    }


    public function getTimesFormatAttr($value, $data)
    {
        if(isset($data['times'])){
            return getVideoTime($data['times']);
        }else{
            return "";
        }
    }

    public function getVideoListAttr($value, $data)
    {
        $pvideo=Pvideo::with(['video'])->where('package_id',$data['id'])->order('id asc')->select();
        return $pvideo;
    }







}
